package com.wms.service.information;

import com.wms.common.PageInfo;
import com.wms.common.Utils;
import com.wms.dao.GoodsMapper;
import com.wms.domain.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class GoodsImpl implements GoodsService {

    private static final Logger log = LoggerFactory.getLogger(GoodsImpl.class);
    @Autowired
    private GoodsMapper goodsMapper;

    @Transactional
    @Override
    public Object addOrUpdateGoods(GoodsDomain goodsDomain) {
        log.info("add goods "+ goodsDomain.toString());
        goodsDomain.setDate(Utils.getDate());
        if(goodsDomain.getId() != null){
            goodsMapper.updateGoods(goodsDomain);
        }else {
            if(goodsDomain.getIs_goods() == 0){
                String number = Utils.getTableNumber("goods",goodsDomain.getParent_number());
                log.info(number);
                goodsDomain.setGoods_number(number);
            }
            goodsMapper.addGoods(goodsDomain);
        }
        return null;
    }

    @Transactional
    @Override
    public Object deleteGoods(Long id) {
        goodsMapper.deleteGoods(id);
        return null;
    }

    @Override
    public Object getGoodsPage(GoodsDomain goodsDomain) {
        log.info(goodsDomain.toString());
        PageInfo page = new PageInfo();
        page.setData(goodsMapper.getGoodsPage(goodsDomain));
        page.setCount(goodsDomain.getBaseCount());
        page.setSum(goodsDomain.getBaseSum());
        return page;
    }
}
